package com.equipment.fkdcn.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.equipment.fkdcn.pojo.EParameter;
import com.equipment.fkdcn.pojo.Repair;
import com.equipment.fkdcn.service.DepartmentServiceImpl;
import com.equipment.fkdcn.service.ETypeServiceImpl;
import com.equipment.fkdcn.service.EparameterServiceImpl;
import com.equipment.fkdcn.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("pj")
public class PageJump {
    private EParmeterController parmeterController;
    @Autowired
    @Qualifier("eparameterService")
    private EparameterServiceImpl eparameterService;
    @Autowired
    @Qualifier("eTypeService")
    private ETypeServiceImpl eTypeService;

    /*部门*/
    @Autowired
    @Qualifier("departmentService")
    private DepartmentServiceImpl dep;


    /*维修处理*/
    @Autowired
    @Qualifier("repairService")
    private RepairService repairService;

    //设备添加页面
    @GetMapping("/pageAdd")
    public  String  add(Model model){
        model.addAttribute("types",eTypeService.AllQueryEType());
        return "/eAdministration/addE";
    }


    //修改设备页面
    @GetMapping("/pageModify")
    public String modify(){
        return  "/eAdministration/modify";
    }


    //类型管理
    @GetMapping("/tm")
    public String tM(Model model){
        model.addAttribute("types",eTypeService.AllQueryEType());
        return  "/eAdministration/typelist";
    }

    //归还列表
    @GetMapping("/still")
    public String lrList(){
        return  "/lendReturn/still";
    }
    // 借出列表
    @GetMapping("/lend")
    public String reList(){
        return "/lendReturn/lend";
    }


    //添加借出页面
    @GetMapping("/addLendPage")
    public String addLend(Model model){
        model.addAttribute("depar",dep.allQuery());//所有部门
        model.addAttribute("etype",eTypeService.AllQueryEType());
        //所有设备
        List<EParameter> list = eparameterService.allEParameter();
        Object toJSON = JSON.toJSONString(list);
        model.addAttribute("ep",toJSON);
        return "/lendReturn/addLend";
    }



    @GetMapping("/register")
    public String register(){   //注册页面
       return "/adminPage/register";
    }

    //添加维修
    @GetMapping("/addRep")
    public String addRep(Model model){
        model.addAttribute("etype",eTypeService.AllQueryEType());
        return "/repair/addRepair";
    }

    //维修列表
    @GetMapping("/repairList")
    public String repairList(Model model){
        List<Repair> repairs = repairService.allQuery();
        ArrayList<JSONObject> jsonObjects = new ArrayList<JSONObject>();
        for (Repair r:repairs){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",r.getId());
            jsonObject.put("deliveryPlace",r.getDeliveryPlace());
            jsonObject.put("giveRepair",r.getGiveRepair());
            jsonObject.put("eName",r.getEpId().geteName());
            jsonObject.put("typeid",r.getEpId().getEtype().getType());
            jsonObject.put("model",r.getEpId().getModel());
            jsonObject.put("reason",r.getReason());
            jsonObject.put("repairman",r.getRepairman());
           /* jsonObject.put("recovery",r.getRecovery());*/
            jsonObjects.add(jsonObject);
        }
        String jsonString = JSON.toJSONString(jsonObjects);
        model.addAttribute("jsons",jsonString);
        return "/repair/repairList";
    }



    //报废列表
    @GetMapping("/srcList")
    public  String scrList(){
        return "/scrapPage/scrapList";
    }

    //测试文件
    @RequestMapping("/menu")
    public String testTop(){

        return "/mainPage/menu";
    }

    //主页面

    @RequestMapping("/index")
    public String mainPage(){
        return "/mainPage/index";
    }

    @RequestMapping("/addRet")
    public String addRe(Model model){//添加归还记录
        model.addAttribute("dep",dep.allQuery());
        model.addAttribute("typeList",eTypeService.AllQueryEType());
        return  "/lendReturn/addRe";
    }

    //产品信息
    @RequestMapping("/pInf")
    public  String  ProductInformation(){
        return  "/mainPage/product";
    }

    //退出登录
    @RequestMapping("/exitLogin")
    public String  exitLogin(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
        session.setAttribute("adminName",null);
        session.setMaxInactiveInterval(0);
        return "/mainPage/menu";
    }

    @RequestMapping("/addScrap")
    public  String  addScrap(){//添加报废
        return "/scrapPage/addScrap";
    }

    
    //头像
    @RequestMapping("/topImage")
    public String topImage() {
		return "/page/topImage";
	}




}
